<?php

class JobController extends Controller
{
	public function actionIndex() {
        $this->render('index');
    }

    public function actionCreate() {
        $model = new Job('register');

        // uncomment the following code to enable ajax-based validation
        /*
          if(isset($_POST['ajax']) && $_POST['ajax']==='job-jobPost-form')
          {
          echo CActiveForm::validate($model);
          Yii::app()->end();
          }
         */
         $succ = false;
        if (isset($_POST['Job'])) {
            $model->attributes = $_POST['Job'];
            if ($model->validate('jobPost')) {
                // form inputs are valid, do something here
                $model->status = 0;
                $model->poster_id = Yii::app()->params['userFb']->getId();
                $model->save();
                $succ = true;
                //die(print_r($model->attributes, true));
            }
        }
        $this->render('jobPost', array('model' => $model, "action"=> "create", "success" => $succ, "auth" => true));
    }

    public function actionEdit($id) {
        $poster = '1052323993';
         $cond = array(
                                'condition' => 'id = :id',
                                'params' => array(':id' => $id),
                            );
        $model = Job::model()->find($cond);
        $auth = false;
        $succ = false;
        
        if ($model->poster_id == $poster){
           $auth = true;
            if (isset($_POST['Job'])) {
                $model->attributes = $_POST['Job'];
                if ($model->validate('jobPost')) {
                    // form inputs are valid, do something here
                    //die(print_r($model->attributes, true));
                    $model->save();
                    $succ = true;
                }
            }
            $this->render('jobPost', array('model' => $model, "action"=> "edit", "success" => $succ, "auth" => $auth));
        }
        else{
            $this->render('jobPost', array('model' => $model, "action"=> "edit", "success" => $succ, "auth" => $auth));
        }

        // uncomment the following code to enable ajax-based validation
        /*
          if(isset($_POST['ajax']) && $_POST['ajax']==='job-jobPost-form')
          {
          echo CActiveForm::validate($model);
          Yii::app()->end();
          }
         */
        
    }

    public function actionView($id) {
        $model = Job::model()->findByPk($id);
        if($model == null) {
            throw new CHttpException(400, "Invalid request");
            Yii::app()->end();
        }

        //render
        $this->render('view', array('model' => $model));
    }

    public function actionDelete($id){
        $poster = '1052323993';
         $cond = array(
                                'condition' => 'id = :id',
                                'params' => array(':id' => $id),
                            );
        $model = Job::model()->find($cond);
        $auth = false;
        $succ = false;

        if ($model->poster_id == $poster){
           $auth = true;
           $model->delete();
           $succ = true;
           $this->redirect(Yii::app()->request->baseUrl. "/index.php?r=site/index/");
        }
         
         $this->render('jobPost', array('model' => $model, "action"=> "edit", "success" => $succ, "auth" => $auth));
    }

    public function actionApply(){
        //check whether id is provided
        if(!isset($_POST['id'])) {
            throw new CHttpException(400, "Invalid Request");
            Yii::app()->end();
        }

        //save the new application
        $app = new Application();
        $app->job_id    = $_POST['id'];
        $app->user_id   = Yii::app()->params['userFb']->getId();
        $app->status    = ConstantsLookUp::$APPSTATUS_WAITING;
        if(!$app->save()) {
            throw new CHttpException(500, "Internal Server Error. Please try again.");
            Yii::app()->end();
        }

        //add new notif
        Notif::applicationNotif($app->job_id, $this);

        //redirect
        if(isset ($_POST['redirectUrl'])) {
            $this->redirect($_POST['redirectUrl']);
        } else {
            $this->redirect($this->createUrl('view', array('id' => $app->job_id)));
        }
    }

    public function actionAcceptApplication() {
        //check whether id is provided
        if(!isset($_POST['id'])) {
            throw new CHttpException(400, "Invalid Request");
            Yii::app()->end();
        }

        //WARNING : DB TRANSACTION REQUIRED!
        //update application
        $app = Application::model()->findByPk($_POST['id']);
        $app->status = ConstantsLookUp::$APPSTATUS_APPROVED;
        if(!$app->save()) {
            throw new CHttpException(500, "Internal Server Error. Please try again.");
            Yii::app()->end();
        }

        //update job status to waiting transaction verification
        $job = Job::model()->findByPk($app->job_id);
        if($job == null) {
            throw new CHttpException(500, 'Internal server error : Inconsistent data, job not found!');
            Yii::app()->end();
        }
        $job->status = ConstantsLookUp::$JOBSTATUS_WAITING_TRANS;
        if(!$job->save()) {
            throw new CHttpException(500, 'Internal server error : Unable to update job status');
            Yii::app()->end();
        }

        //redirect
        if(isset ($_POST['redirectUrl'])) {
            $this->redirect($_POST['redirectUrl']);
        } else {
            $this->redirect($this->createUrl('view', array('id' => $app->job_id)));
        }
    }

    public function actionRejectApplication() {
        //check whether id is provided
        if(!isset($_POST['id'])) {
            throw new CHttpException(400, "Invalid Request");
            Yii::app()->end();
        }

        //update application
        $app = Application::model()->findByPk($_POST['id']);
        $app->status = ConstantsLookUp::$APPSTATUS_REJECTED;
        if(!$app->save()) {
            throw new CHttpException(500, "Internal Server Error. Please try again.");
            Yii::app()->end();
        }

        //redirect
        if(isset ($_POST['redirectUrl'])) {
            $this->redirect($_POST['redirectUrl']);
        } else {
            $this->redirect($this->createUrl('view', array('id' => $app->job_id)));
        }
    }

    // Uncomment the following methods and override them if needed
    /*
      public function filters()
      {
      // return the filter configuration for this controller, e.g.:
      return array(
      'inlineFilterName',
      array(
      'class'=>'path.to.FilterClass',
      'propertyName'=>'propertyValue',
      ),
      );
      }

      public function actions()
      {
      // return external action classes, e.g.:
      return array(
      'action1'=>'path.to.ActionClass',
      'action2'=>array(
      'class'=>'path.to.AnotherActionClass',
      'propertyName'=>'propertyValue',
      ),
      );
      }
     */
}